<template>
  <div class="login">
    <p>登录</p>
    <div class="mask">
      <div class="img">
        <img src="../img/后台/后台登录页面大logo.png" alt="" />
      </div>
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="user"
            placeholder="请输入手机号"
            :rules="[{ required: true, message: '请输入正确的手机号' }]"
            class="name"
            :type="number"
            @focus="open"
            @blur="close"
            left-icon="user-circle-o"
          />
          <van-progress
            :show-pivot="false"
            color="#ee0a24"
            :percentage="num"
            stroke-width="1"
          />
          <van-field
            v-model="password"
            :type="type"
            name="pwd"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
            class="pwd"
            autocomplete
            @focus="open2"
            @blur="close2"
            left-icon="records"
          />
          <van-progress
            :show-pivot="false"
            color="red"
            :percentage="num2"
            stroke-width="1"
          />
        </van-cell-group>
        <div style="margin: 16px" class="box">
          <van-button
            round
            block
            color="#ff5e81"
            native-type="submit"
            class="btn"
          >
            登录
          </van-button>
          <van-button tynpe="default" round block @click="back"
            >取消</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { showSuccessToast, showFailToast } from "vant";
import { Login } from "../api/index";
import { useCounterStore } from "../stores/counter";

const stores2 = useCounterStore();
const router = useRouter();
const number = ref("");
const username = ref("");
const password = ref("");
console.log(import.meta.env.VITE_APP_BASE_API==='http://43.136.217.92:8080/')
const onSubmit = () => {
  // console.log(username.value, password.value)
  Login(username.value, password.value)
    .then((res) => {
      // console.log(res, "+++++++");
      if (res.data.code === 20000) {
        const token = res.data.data.token.eid;
        const etel = res.data.data.token.etel;
        localStorage.setItem("etel", etel);
        localStorage.setItem("token", token);
        stores2.loginArr = res.data.data.token;
        showSuccessToast("登录成功！！");
        router.replace({
          path: "/",
        });
      } else if (res.data.code === 50000) {
        showFailToast("登录失败！请检查账号密码是否正确！");
      }
    })
    .catch((err) => {
      showFailToast("登录失败！请检查检查！");
      // console.log(err, '-------');
    });
};

const type = ref("passwrod");
const num = ref(0);
const num2 = ref(0);

const time = ref(null);
const time2 = ref(null);

function open() {
  time.value = setInterval(() => {
    if (num.value < 100) {
      num.value += 20;
    }
  }, 1);
}
function close() {
  clearInterval(time.value);
  num.value = 0;
}

function open2() {
  time2.value = setInterval(() => {
    if (num2.value < 100) {
      num2.value += 20;
    }
  }, 1);
}
function close2() {
  clearInterval(time2.value);
  num2.value = 0;
}

function back() {
  router.back();
}
</script>

<style lang="less" scoped>
.login {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(180deg, aqua, rgb(17, 187, 187));

  // color: aqua;
  p {
    padding-top: 4.8vw;
    font-size: 7vw;
    margin-left: 5vw;
    color: #ffffff;
  }

  .mask {
    width: 90vw;
    height: 80vh;
    margin: 7.733vw 0 0 5vw;
    border: 0.133vw solid gray;
    background-color: white;
    border-radius: 10px;
    position: relative;

    .box {
      // background: red;
      position: absolute;
      width: 90%;
      bottom: 1vw;
    }

    .img {
      width: 20vw;
      height: 16vw;
      margin: 8vw auto 14vw;

      img {
        width: 100%;
      }
    }

    button {
      margin-top: 2.667vw;
      border: 1px solid #ff5e81;
    }

    .name {
      border-bottom: 0.133vw solid gray;
    }

    .pwd {
      border-bottom: 0.133vw solid gray;
      // margin-bottom: 13.333vw;
    }

    .btn {
      margin-top: 13.3vw;
    }
  }
}
</style>
